//
// Created by 31150 on 2024/6/9.
//
# include<bits/stdc++.h>
# include "tools/tools.h"
using namespace std;

// 查找小于等于target的最大值下标
int binary_search(vector<int>& nums, int target) {
    int l = 0, r = nums.size() - 1; // [l, r]
    while (l <= r) {
        int m = l + ((r - l) >> 1);
        if (nums[m] >= target) {
            r = m - 1;
        } else if (nums[m] < target) {
            l = m + 1;
        } else {
            return m;
        }
    }
    return l;
}

int main() {
    vector<int> data = {1, 3, 5, 7, 9};
    debug(binary_search(data, 0));
    debug(binary_search(data, 1));
    debug(binary_search(data, 2));
    debug(binary_search(data, 3));
    debug(binary_search(data, 4));
    debug(binary_search(data, 5));
    debug(binary_search(data, 6));
    debug(binary_search(data, 7));
    debug(binary_search(data, 8));
    debug(binary_search(data, 9));
    debug(binary_search(data, 10));
}
